<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\common\model\TemplateHeaderModel;
use app\common\logic\admin\TemplateHeader;
/*
	表头
 */
class TemplateHeaderController extends AdminBaseController
{
	//列表
	public function index(){
		$data = TemplateHeaderModel::order('sort asc,header_id desc')->paginate(20);
		$this->assign('data',$data);
		$this->assign('page',$data->render());
		return $this->fetch();
	}
	//添加
	public function add(){
		return $this->fetch();
	}
	//提交添加
	public function toAdd(TemplateHeader $TemplateHeader){
		$param = $this->request->param();
		if (in_array('', $param['post']['content'])) {
			$this->error('字段不能为空');die;
		}
		$rs = $TemplateHeader->save($param);

		if ($rs) {
			$this->success('保存成功','index');
		}else{
			$this->error('保存失败');
		}
	}
	//编辑
	public function edit(){
		$param = $this->request->param();
		$data = TemplateHeaderModel::get($param['id']);
		$data['content'] = json_decode($data['content'],true);
		
		$this->assign('data',$data);
		return $this->fetch();
	}
	//提交编辑
	public function toEdit(TemplateHeader $TemplateHeader){
		$param = $this->request->param();
		if (in_array('', $param['post']['content'])) {
			$this->error('字段不能为空');die;
		}
		$rs = $TemplateHeader->save($param,2);

		if ($rs) {
			$this->success('保存成功','index');
		}else{
			$this->error('保存失败');
		}
	}
	//设置
	public function set(){
		$param = $this->request->param();
		$rs = TemplateHeaderModel::where('header_id',$param['header_id'])->update(['status'=>$param['status']]);
		if ($rs) {
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
	}
	//批量设置
	public function sets(){
		$param = $this->request->param();
		$ids = implode($param['ids'],',');
		$rs = TemplateHeaderModel::where('header_id','in',$ids)->update(['status'=>$param['status']]);
		if ($rs) {
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
	}
	//预览
	public function look(){
		$param = $this->request->param();
		$data = TemplateHeaderModel::get($param['id']);
		$data['content'] = json_decode($data['content'],true);
		$this->assign('data',$data);
		return $this->fetch();
	}
	//排序
	public function sort(){
		$param = $this->request->param();
		foreach($param['sort'] as $key => $value){
			TemplateHeaderModel::where('header_id',$key)->update(['sort'=>$value]);
        }
        $this->success('操作成功!');
	}
}
